package com.example.lotterysystem.service.activitystatus.operator;

import com.example.lotterysystem.dao.dataobject.ActivityDO;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;
    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //需要判断奖品是否全部抽完
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityStatusEnum activityTargetStatus = convertActivityStatusDTO.getActivityTargetStatus();
        if(null == activityId || null == activityTargetStatus) {
            return false;
        }
        ActivityDO activityDO = activityMapper.selectById(activityId);
        if(null == activityDO) {
            return false;
        }
        // 当前活动状态与传入的状态一致，不处理
        if(activityTargetStatus.name().equalsIgnoreCase(activityDO.getStatus())) {
            return false;
        }
        //判断是否奖品已被抽取完
        if(activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name()) > 0) {
            return  false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            activityMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getActivityTargetStatus().name());
            return true;
        } catch (Exception e) {
            return false;
        }

    }
}
